<?
require_once('common.php');
require_once('../bin/db.php');

function options($from,$to,$select) {
	$html='';
	for ($i=$from;$i<=$to;$i++) {
		if ($i==$select) $sel=' selected';
			else $sel='';
		$html.='<option value="'.$i.'"'.$sel.'>'.$i.'</option>';
	}
	return $html;	
}


$method=$_GET['method'];
$search=$_GET['search'];

$db= new MySQL_Class();
$db->Create();

$where='';

if ($method=='product') {
	
	if (null==$fd) $fd=date('d');
	if (null==$fm) $fm=date('m');
	if (null==$fy) $fy=date('Y');
	if (null==$td) $td=date('d');
	if (null==$tm) $tm=date('m');
	if (null==$ty) $ty=date('Y');
	
	$form='<table border="0" width="100%"><tr><td width="50%"><form name="form1" method="GET" action="">
	<input type="hidden" name="search" value="'.$search.'">
	<input type="hidden" name="method" value="'.$method.'">
	<input type="hidden" name="step" value="2">
	<table border="0" class="searchbox">
        <tr bgcolor="#CCCCCC"> 
          <td><div align="right">Transaction Date (DD-MM-YYYY)</div></td>
          </tr>
        <tr> 
          <td><div align="right">From :
            <select name="fd">
					'.options(1,31,$fd).'
            </select> 
              -
              <select name="fm">
					'.options(1,12,$fm).'
              </select>
              -
              <select name="fy">
					'.options(2004,2010,$fy).'
              </select>
          </div></td>
          </tr>
        <tr> 
          <td><div align="right">To :
            <select name="td">
				'.options(1,31,$td).'
            </select>
  -
  <select name="tm">
'.options(1,12,$tm).'
  </select>
  -
  <select name="ty">
'.options(2004,2010,$ty).'
  </select>
          </div></td>
          </tr>
		  <tr><td align="center">
		  <input name="Submit" type="submit" class="button" value="  Go  "> 
          <input name="listall" type="button" class="button" id="listall" onClick="location.href=\''.$PHP_SELF.'?search='.$search.'&method='.$method.'&all=all\';" value="List All">
		  </td></tr>
      </table>
	  </form></td><td align="right">
	  <a href="../report/export_trans.php?search='.$search.'&method='.$method.'"><img src="../images/excel.gif" border="0"></a>
	  </td></tr></table>
	';
	
	if ($all=='all') {
		$where="where pid=$search";
	}
	else if ($step==2) {
		$ftime=strtotime($fy.'-'.$fm.'-'.$fd.' 00:00:00');
		$ttime=strtotime($ty.'-'.$tm.'-'.$td.' 23:59:59');
		$where="where pid=$search and trans_time between $ftime and $ttime";
	}
	$sql="select name,brand from pro_basic where id=$search;";
	$db->Query($sql);
	$rec=mysql_fetch_object($db->result);
	$title="Transaction records of product :<br> ".$rec->name."<br>".$rec->brand;
}

if ($method=='person') {
	$where=" where person='$search'";
	$title=$search."'s transaction records";
}

if ($where) {
	$sql="select * from pro_transaction $where order by id asc";
	$db->Query($sql);

	$html='<div class="mytable"><font color="#ff3333">*</font> = Cancelled transaction</div><br />
	<table width="100%"  border="0" cellpadding="3" cellspacing="0" class="mytable">
  <tr bgcolor="#3399FF">
    <td width="10%"><div align="center" class="style1">Order Qty </div></td>
    <td width="10%"><div align="center" class="style1">Cancelled Qty </div></td>
    <td width="10%"><div align="center" class="style1">Re-stock Qty </div></td>
    <td width="25%"><div align="center" class="style1">Person</div></td>
    <td width="30%"><div align="center" class="style1">Transaction Date </div></td>
    <td width="15%"><div align="center" class="style1">Order No. </div></td>
  </tr>';
	$res=$db->result;
	$sumord=0;
	$sumstk=0;
	$sumcan=0;
	while ($rec=mysql_fetch_object($res)) {
	
		if (is_numeric($rec->person)) {
			$sql="select * from cus_info where id=".$rec->person;
			$db->Query($sql);
			$r=mysql_fetch_object($db->result);
			$person=$r->firstname.' '.$r->lastname.' ['.str_pad($r->id,5,'0',STR_PAD_LEFT).']';
			$restock='';
			$purchase=abs($rec->amount);
			$color='#CCCCCC';	
			$canceldqty='';
		}
		else {
			$person=$rec->person;
			$restock=$rec->amount;
			$purchase='';
			$color='#EEEEEE';
			$canceldqty='';
		}
//cancelled order 
		if ($rec->cancelled) {
			$cancel=' class="cancelled"';
			$color='#DDDDDD';
			$sumcan+=$purchase;
			$canceldqty=' <font color="#ff3333">*</font> '.$purchase;
			$purchase='';
		}
		else $cancel='';		

		if ($purchase) $sumord+=$purchase;
		if ($restock) $sumstk+=$restock;
	

//order list	
		if ($rec->orderno) $orderno=str_pad($rec->orderno,5,'0',STR_PAD_LEFT);
		else $orderno='';
		
		$html.='<tr bgcolor="'.$color.'"'.$cancel.'>
				<td align="center">'.$purchase.'</td>
				<td align="center">'.$canceldqty.'</td>
				<td align="center">'.$restock.'</td>
				<td align="center">'.$person.'</td>
				<td align="center">'.date('Y-m-d H:i:s',$rec->trans_time).'</td>
				<td align="center">'.$orderno.'</td></tr>';
	
	}
	$html.="</table><br /> <div class=\"boldtext\">Total Order Qty : $sumord <br /> Total Restock Qty : $sumstk <br /> Total Cancelled Qty : $sumcan</div>";
} 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Transactions</title>
<style type="text/css">
<!--
..button {  COLOR: #000000; BACKGROUND-COLOR: #FDD970; border:1px solid; font-family: "verdana"; font-size:11 px; border-color:  #FFFFFF #CC6600 #CC6600 #FFFFFF}
.tbheader {
	color: #FFFFFF;
	background-color: #6890DB;
	text-align: center;
	font-weight: bold;

}
.tbcontent {
	background-color: #FFFFFF;
}
.tbcontent1 {
	background-color: #FFFFFF;
}
.tbcontent2 {
	background-color: #FFFFFF;
}

.tdheader {
	font-weight: bold;
	vertical-align: top;
}
.mytable {
	font-family: "Trebuchet MS";
	font-size: 9pt;
	font-style: normal;
}
.boldtext {
	font-family: "Trebuchet MS";
	font-size: 9pt;
	font-style: normal;
}
.title {
	color: #000066;
	font-weight: bold;
	font-family: Arial;
	font-size: 12pt;
}
.searchbox {
	font-family: Arial;
	font-size: 9pt;
}
.cancelled {
	color: #999999
}
-->
</style>
</head>

<body>
<p class="title"><?=$title?></p><br /><?=$form?>
  <?=$html?>
  <br />

</body>
</html>
